package marchsoft.modules.selfFish.pisciculture.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import marchsoft.exception.BadRequestException;
import marchsoft.modules.selfFish.pisciculture.entity.dto.HealthDto;
import marchsoft.modules.selfFish.pisciculture.service.HealthService;
import marchsoft.response.Result;
import marchsoft.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**健康监测信息
 * @title HealthController
 * @description 健康监测信息
 * @author Lenovo
 * @version 1.0
 * @create 2024-07-15 16:53
 */
@Slf4j
@RestController
@RequestMapping("/api/health")
@Api(tags = "养鱼管理：健康管理")
public class HealthController {

    @Autowired
    private HealthService healthService;

    private static final String ENTITY_NAME = "health";

    @GetMapping
    @ApiOperation("获取鱼群健康列表")
    @PreAuthorize("@smpe.check('health:list')")
    public Result<Object> query(String fishName,
                                @RequestParam(value ="pageNum",defaultValue = "1")Long pageNum,
                                @RequestParam(value ="pageSize",defaultValue = "10") Long pageSize ){
        log.info(StrUtil.format("【获取鱼群健康列表 /api/health】操作人id：{}，鱼群健康查询条件 healthName：{}，分页pageNum：{}", SecurityUtils.getCurrentUserId(),fishName, pageNum));
        return healthService.getHealthList(fishName,pageNum,pageSize);
    }

    @PostMapping
    @ApiOperation("新增鱼群健康")
    @PreAuthorize("@smpe.check('health:add')")
    public Result<Object> create(@RequestBody HealthDto healthDto){
        log.info(StrUtil.format("【新增鱼群健康 /api/health】操作人id：{}，新增鱼群健康实体：{}", SecurityUtils.getCurrentUserId(), healthDto));
        if (ObjectUtil.isNotNull(healthDto.getId())) {
            log.error(StrUtil.format("【新增鱼群健康失败】操作人id：{}，新增鱼群健康实体默认id应该为空，dept：{}", SecurityUtils.getCurrentUserId(), healthDto));
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        return healthService.create(healthDto);
    }

    @DeleteMapping
    @ApiOperation("删除鱼群健康")
    @PreAuthorize("@smpe.check('health:del')")
    public Result<Object> delete(@RequestBody Set<Long> ids){
        log.info(StrUtil.format("【删除鱼群健康 /api/health】操作人id：{}，被删除鱼群健康id集：{}", SecurityUtils.getCurrentUserId(), ids.toString()));
        return healthService.delete(ids);
    }

    @PutMapping
    @ApiOperation("修改鱼群健康")
    @PreAuthorize("@smpe.check('health:edit')")
    public Result<Object> update(@RequestBody HealthDto healthDto){
        log.info(StrUtil.format("【修改鱼群健康 /api/health】操作人id：{}，被修改鱼群健康id：{}", SecurityUtils.getCurrentUserId(), healthDto.getId()));
        return healthService.update(healthDto);
    }

    @ApiOperation("导出鱼群健康数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@smpe.check('health:list')")
    public void download(HttpServletResponse response, String fishName) throws IOException {
        log.info(StrUtil.format("【查询鱼群健康 /api/health】操作人id：{}，鱼群健康查询条件 fishName：{}", SecurityUtils.getCurrentUserId(), fishName));
        healthService.download(fishName,response);
    }
}
